/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monitoring.ws;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import monitoring.beans.util.FilterBean;
import monitoring.logic.Export;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;

/**
 * REST Web Service
 *
 * @author Regulus
 */
@Path("export")
public class ExportWS {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of ExportWS
     */
    public ExportWS() {
    }

    @GET
    @Path("/monitoring/{jsonParam}")
    @Produces("application/json")
    public String javajson(@PathParam("jsonParam") String jsonParam) throws JSONException {      
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.alias("filter", FilterBean.class);
        List<FilterBean> list = jsonParam.length()<6?new ArrayList<FilterBean>():(ArrayList<FilterBean>) xstream.fromXML(jsonParam);
        Export logic = new Export();        
        ExportMessage expo;
        try {
            expo = logic.export(list);            
        } catch (SQLException ex) {
            Logger.getLogger(ExportWS.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        }    catch (Exception ex) {
            Logger.getLogger(ExportWS.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        }
    
        XStream xstream2 = new XStream(new JettisonMappedXmlDriver());
        xstream2.setMode(XStream.NO_REFERENCES);
        xstream2.alias("exportMessage", ExportMessage.class);
        return xstream2.toXML(expo);
    }

    @GET
    @Path("/imprimeJson/{jsonParam}")
    @Produces("application/json")
    public String javajjson(@PathParam("jsonParam") String jsonParam) throws JSONException {

        XStream xstream2 = new XStream(new JettisonMappedXmlDriver());
        xstream2.setMode(XStream.NO_REFERENCES);
        xstream2.alias("filter", FilterBean.class);
        List<FilterBean> list = new ArrayList<FilterBean>();
        
        FilterBean f = new FilterBean();
        f.setEntity("institutioncode");        
        f.setValue("135");
        
        
        FilterBean f1 = new FilterBean();
        f1.setEntity("collectioncode");
        f1.setValue("5");
        
        list.add(f);
        //list.add(f1);
        return xstream2.toXML(list);


    }
}
